/**
 * This program is an easy to use FTP client,
 * my idea is that a tcp socket connection will be made between
 * the client and server using only 2 email exchanges
 */

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class EasyFTP {
	static JLabel port1 = null;
	BufferedImage image;
	static String privKey = null;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		EasyFTP ftp = new EasyFTP();
		KeyGeneration key = new KeyGeneration();
		ArrayList<Character> privateKey = key.generatePrivateKey();
		privKey = key.getKey();
		ftp.setupFolders();
		GUI gui = new GUI();
		ftp.guiSetup(gui);
		gui.setVisible(true);
	}
	
	public void setupFolders(){
		String filepath = System.getProperty("user.home")+"\\EasyFTP";
		String saved = System.getProperty("user.home")+"\\EasyFTP\\Saved";
		File fp = new File(filepath);
		File s = new File(saved);
		if(!fp.exists())
			fp.mkdir();
		if(!s.exists())
			s.mkdir();
	}
	
	
	public void guiSetup(GUI g){
		JPanel panel = new JPanel(new GridLayout(6,2,2,2));
		//Set minimum dimensions to the window
		Dimension minimum = new Dimension(400,400);
		g.setMinimumSize(minimum);
		//Set location of pop up
		g.setLocationRelativeTo(null);
		//Set to close and be visible
		g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		g.setVisible(true);
		ConnectionSetup setup = new ConnectionSetup();
		JLabel ip = new JLabel("Your IP address is:");
		panel.add(ip);
		JLabel ip1 = new JLabel(setup.getIP());
		panel.add(ip1);
		JLabel port = new JLabel("Current port used:");
		panel.add(port);
		port1 = new JLabel(""+setup.getPort());
		panel.add(port1);  
		
		/*
		 * Make the front four buttons on the GUI
		 */
		JButton sendRequest = new JButton("Send Request");
		sendRequest.addActionListener(new ActionListener(){

			/* (non-Javadoc)
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			@Override
			public void actionPerformed(ActionEvent e) {
				LoginPrompt prompt = new LoginPrompt();
				prompt.makeFrame();
								
			}
			
		});
		panel.add(sendRequest);
		
		JButton importBtn = new JButton("Import Setup");
		importBtn.addActionListener(new ImportSetup());
		panel.add(importBtn);
		
		JButton generate = new JButton("Pick Custom Port");
		generate.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				String s = (String)JOptionPane.showInputDialog(null, "Please input new port:", "New Port Number", JOptionPane.OK_CANCEL_OPTION);
				if(s != null){
					ConnectionSetup setup = new ConnectionSetup();
					setup.setPort(s);
					updatePort(setup);
				}
			}
			
		});
		panel.add(generate);
		
		JButton auth = new JButton("Google 2-Way Auth");
		auth.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				RadioAuthSelect auth = new RadioAuthSelect();
				auth.makeFrame();
			}
			
		});
		panel.add(auth);
		
		JButton uploadUser = new JButton("Upload User");
		uploadUser.addActionListener(new ImportUser());
		
	
		panel.add(uploadUser);
		
		JButton ex = new JButton("Exit");
		ex.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
				
			}
			
		});
		panel.add(ex);
		
		
		JLabel name = new JLabel("Created by Alex Meyer");
		JLabel gmail = new JLabel("Powered by Google GMail");
		panel.add(name);
		panel.add(gmail);
		/*
		 * Place logo here, needs some work
		 */
		//ImageIcon image = new ImageIcon("gmail.png");
		//JLabel label = new JLabel("", image, JLabel.CENTER);
		//panel.add(label);
		
		//Create the Menu Bar
		JMenuBar menuBar = new JMenuBar();
		//Create things to go on Menu Bar
		JMenu fileMenu = new JMenu("File");
		JMenu advanced = new JMenu("Advanced");
		//These go under File
		JMenuItem request = new JMenuItem("Send Request");
		JMenuItem upload = new JMenuItem("Import Setup");
		JMenuItem exit = new JMenuItem("Exit");
		
		//Set up listeners
		request.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				ConnectionSetup setup = new ConnectionSetup();
				setup.getIP();
				
			}
			
		});
		upload.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				
			}
			
		});
		exit.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
			
		});
		//Adding Menu Items to File
		fileMenu.add(request);
		fileMenu.add(upload);
		fileMenu.add(exit);
		//Ability to set up a new port to transmit or receive on
		JMenuItem setPort = new JMenuItem("Set Custom Port");
		advanced.add(setPort);
		setPort.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				String s = (String)JOptionPane.showInputDialog(null, "Please input new port:", "New Port Number", JOptionPane.OK_CANCEL_OPTION);
				if(s != null){
					ConnectionSetup setup = new ConnectionSetup();
					setup.setPort(s);
					updatePort(setup);
				}
			}
			
		});
		JMenuItem setAuth = new JMenuItem("Set GMail 2-way Auth");
		advanced.add(setAuth);
		setAuth.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				RadioAuthSelect auth = new RadioAuthSelect();
				auth.makeFrame();
			}
			
		});
		JMenuItem up = new JMenuItem("Upload User");
		advanced.add(up);
		up.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				/*
				 * Uploading user crap
				 */
				
			}
			
		});
		//Add components to Menu Bar
		menuBar.add(fileMenu);
		menuBar.add(advanced);
		g.setJMenuBar(menuBar);
		menuBar.setVisible(true);
		g.setTitle("EasyFTP");
		g.add(panel);
	}
	
	public void updatePort(ConnectionSetup con){
		port1.setText(""+con.getPort());
	}

}
